package com.example.supermarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.supermarket.Result;
import com.example.supermarket.entity.Category;
import com.example.supermarket.entity.Department;
import com.example.supermarket.entity.Goods;
import com.example.supermarket.service.CategoryService;
import com.example.supermarket.service.DepartmentService;
import com.example.supermarket.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hpp
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    GoodsService goodsService;
    @GetMapping("")
    public Result<List<Category>> getCateList(){
        List<Category> list = categoryService.list();
        if (list.size() != 0) {
            return new Result<>(200,"ok",list);
        }
        return new Result<>(0,"error",null);
    }
    /*返回所有的分类列表*/
    @GetMapping("/split")
    public Result<List<List<Category>>> getCateSplitList() {
        List<List<Category>> totalList = new ArrayList<>();
        Integer len = departmentService.count();
        List<Department> depList = departmentService.list();
        List<Integer> depIdList = new ArrayList<>();
        for (Department department : depList) {
            depIdList.add(department.getDepId());
        }
        for (Integer i = 0; i < len; i++) {
            QueryWrapper<Category> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dep_id",depIdList.get(i));
            List<Category> list1 = categoryService.list(queryWrapper1);
            boolean add = totalList.add(list1);
            System.out.println(add);
        }
        if (!totalList.isEmpty()) {
            return new Result<>(200,"ok",totalList);
        }
        return new Result<>(0,"error",null);
    }
    /*以级联的方式返回指定层次的分区分类商品列表*/
    @GetMapping("/allList")
    public Result<List<Department>> getDepCateGoodsList(@RequestParam("type") Integer type) {
        List<Department> deplist = departmentService.list();
        QueryWrapper<Category> catequeryWrapper = new QueryWrapper<>();
        QueryWrapper<Goods> goodsqueryWrapper = new QueryWrapper<>();
        for (Department department : deplist) {
            department.setValue(department.getDepId());
            department.setLabel(department.getDepName());
            catequeryWrapper.eq("dep_id",department.getDepId());
            List<Category> catelist = categoryService.list(catequeryWrapper);
            department.setChildren(catelist);
            for (Category category : catelist) {
                category.setValue(category.getCateId());
                category.setLabel(category.getCateName());
                if (type == 3) {
                    goodsqueryWrapper.eq("cate_id",category.getCateId());
                    List<Goods> goodslist = goodsService.list(goodsqueryWrapper);
                    category.setChildren(goodslist);
                    for (Goods goods : goodslist) {
                        goods.setValue(goods.getGdId());
                        goods.setLabel(goods.getGdName());
                    }
                }
            }
        }
        return new Result<>(200,"ok",deplist);
    }
}

